﻿unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  IdHashCRC, IdHashSHA, IdSSLOpenSSLHeaders,CnMD5;

type
  TForm1 = class(TForm)
    Btnv_1: TButton;
    Btnv_2: TButton;
    Btnv_3: TButton;
    procedure Btnv_1Click(Sender: TObject);
    procedure Btnv_2Click(Sender: TObject);
    procedure Btnv_3Click(Sender: TObject);
  private
  public
    function GetFile_MD5(const iFileName: String): String;    //获取文件MD5
    function GetFile_SHA1(const iFileName: String): String;   //获取文件SHA1
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzDebug,Class_KzUtils;

{$R *.dfm}

procedure TForm1.Btnv_1Click(Sender: TObject);
var
  OD:TOpenDialog;
begin
  try
    OD:=TOpenDialog.Create(nil);
    if OD.Execute then
    begin
      KzDebug.FileLog(GetFile_SHA1(OD.FileName));
    end;
  finally
    FreeAndNil(OD);
  end;
end;

procedure TForm1.Btnv_2Click(Sender: TObject);
begin
  TKzUtils.Explore;
end;

procedure TForm1.Btnv_3Click(Sender: TObject);
var
  OD:TOpenDialog;
begin
  try
    OD:=TOpenDialog.Create(nil);
    if OD.Execute then
    begin
      KzDebug.FileLog(CnMD5.MD5Print(MD5File(OD.FileName)));
    end;
  finally
    FreeAndNil(OD);
  end;
end;

function TForm1.GetFile_MD5(const iFileName: String): String;
{@var
  md5 : IMD5;
  fileStream: TMemoryStream;}
begin
  {@fileStream := TMemoryStream.Create;
  try
    fileStream.LoadFromFile(iFileName);
    md5 := GetMD5;
    md5.Init;
    md5.Update(fileStream.Memory, fileStream.Size);
    Result := md5.AsString;
  finally
    FreeAndNil(fileStream);
  end;}
end;

function TForm1.GetFile_SHA1(const iFileName: String): String;
var
  SHA1      : TIdHashSHA1;
  fileStream: TMemoryStream;
begin
  SHA1       := TIdHashSHA1.Create;
  fileStream := TMemoryStream.Create;
  try
    fileStream.LoadFromFile(iFileName);
    Result := SHA1.HashStreamAsHex(fileStream);
  finally
    fileStream.Free;
    SHA1.Free;
  end;
end;

end.
